package com.yao.util;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;

import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.*;
import com.yao.util.RandomFour;
import org.springframework.stereotype.Component;

@Component
public class SendSms {
    public String send(String phone) {
        String code = RandomFour.random();
        try {

            Credential cred = new Credential("AKIDAE5jAPRFrFPZZJo5D8wYyCD0Yu54pMfk", "tXyEF1YqujKispZ5OTJuIXXJrDOVO4Wm");

            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("sms.tencentcloudapi.com");

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile);

            SendSmsRequest req = new SendSmsRequest();
            String[] phoneNumberSet1 = {"+86" + phone};
            req.setPhoneNumberSet(phoneNumberSet1);

            req.setSmsSdkAppId("1400520528");
            req.setSignName("妖悲冥公众号");
            req.setTemplateId("959640");

            String[] templateParamSet1 = {code, "5"};
            req.setTemplateParamSet(templateParamSet1);


            SendSmsResponse resp = client.SendSms(req);

            System.out.println(SendSmsResponse.toJsonString(resp));
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
        return code;
    }

}
